(function (mui, $) {
var shares = {};

mui.plusReady(function () {
	var view = plus.webview.currentWebview();
	_Data.activity_id = view.extras.id;
	
	// 初始化分享服务
	plus.share.getServices(function (services) {
		if (services && services.length > 0) {
			for (var i = 0; i < services.length; i++) {
				var item = services[i]; shares[item.id] = item;
			}
		}
	}, function() {
		app.log("获取分享服务列表失败");
	});
	
	app.log('查看激动详情：', {'token':app.store('token'), 'data':{'activity_id':view.extras.id}});
	plus.nativeUI.showWaiting();
	$.ajax({
		'dataType' : 'json',
		'type'     : 'post',
		'url'      : app.url('App/Activity/Index/detail'),
		'data'     : {'token':app.store('token'), 'data':{'activity_id':view.extras.id}},
	})
	.fail(function (res) {
		// 失败回调
		plus.nativeUI.closeWaiting();
		app.log('取得活动详情失败：', res);
		app.toast('取得活动详情失败');
	})
	.done(function (res) {
		// 成功回调
		plus.nativeUI.closeWaiting();
		app.log('活动详情：', res);
		
		if (500 == res.code) { app.toast('你还未登录'); return; }
		if (300 == res.code) { app.toast(res.msg); return; }
		if (200 != res.code) { app.toast('请求失败'); return; }
		if (res.msg) { app.toast(res.msg); }
		
		var data = res.data;
		$('.act-title').text(data.title);
		$('.act-desc').text(data.description);
		$('.act-endat').text(data.end_time);
		$('.act-text').html(data.content);
		
		
		$('.mui-slider-group').html(app.tpl('tpl-slider', res.data));
		mui('.mui-slider').slider({interval:5000});
		mui.previewImage();
	})
	;
});
$('body').scroll(function (e) {
	app.log('滚动位置：', e.x);
});

// 我要来秀
$('.btn-join-show').on('tap', function () {
	var view = plus.webview.currentWebview();
	app.open('show.join.html', {'id':view.extras.id});
});

$('.sroll-tab-menu>li').on('tap', function (e) {
	var prev = $(this).parent().find('.active').attr('data-target');
	var next = $(this).attr('data-target');
	
	if (prev == next) { return; }
	
	$(this).removeClass('active').addClass('active').siblings().removeClass('active');
	$('.show-holder' + next).removeClass('active').addClass('active').siblings().removeClass('active');
});

var _Data = {
	'token'       : app.store('token'),
	'page'        : 0,
	'nums'        : 20,
	'target'      : '.show-holder.active',
};

function next (cb) {
	var page = parseInt($(_Data.target).attr('data-page') || '0', 10) + 1;
	
	var data = {
		'activity_id' : _Data.activity_id,
		'order'       : JSON.parse($(_Data.target).attr('data-order') || '{}'),
		'is_images'   : $(_Data.target).attr('data-images'),
	};
	app.log('请求参数：', mui.extend({}, _Data, {'page':page, 'data':data}));
	
	if (plus.webview.currentWebview().isVisible()) { plus.nativeUI.showWaiting(); }
	$.ajax({
		'dataType' : 'json',
		'type'     : 'post',
		'url'      : app.url('App/Activity/Index/showLists'),
		'data'     : mui.extend({}, _Data, {'page':page, 'data':data}),
	})
	.fail(function (res) {
		// 失败回调
		plus.nativeUI.closeWaiting();
		app.log('取得秀图列表失败', res);
		app.toast('取得秀图列表失败');
		app.tryResetPullLoading();
	})
	.done(function (res) {
		// 成功回调
		plus.nativeUI.closeWaiting();
		app.log('秀图列表：', res);
		
		if (500 == res.code) { app.toast('你还未登录'); app.tryResetPullLoading(); return; }
		if (300 == res.code) { app.toast(res.msg); return; }
		if (200 != res.code) { app.toast('请求失败'); return; }
		if (res.msg && plus.webview.currentWebview().isVisible()) { app.toast(res.msg); }
		
		$(_Data.target).attr('data-page', page);
		//if (res.data && res.data.length) { _Data.page++; }
		cb && cb(res);
	})
	;
}

function call_on_pull_down () {
	var pullrefresh = mui('.container-pull-refresh').pullRefresh();
	_Data.page = 0;
	$(_Data.target).empty();
	
	next (function (res) {
		var dom = $(pullrefresh.element).find('.mui-scroll>.scroll-data');
		$(_Data.target).append(app.tpl($(_Data.target).attr('data-tpl'), res));
		pullrefresh.endPulldownToRefresh(); 
	});
}
function call_on_pull_up () {
	var pullrefresh = mui('.container-pull-refresh').pullRefresh();
	
	next (function (res) {
		var dom = $(pullrefresh.element).find('.mui-scroll>.scroll-data');
		$(_Data.target).append(app.tpl($(_Data.target).attr('data-tpl'), res));
		pullrefresh.endPullupToRefresh();
	});
}
mui.init({
	pullRefresh   : {
		container : '.container-pull-refresh',
		//down      : {callback : call_on_pull_down},
		up        : {callback : call_on_pull_up},
	}
});

/*
// 动态修改TAB行的位置
$('.mui-scroll-wrapper').on('dragend', function () {
	setTimeout(function () {
		app.log($('.mui-scroll').length);
		app.log($('.mui-scroll').css('style'));
	}, 500);
});
*/

// 分享
function _doShareMessage (share, ex) {
	var view = plus.webview.currentWebview();
	var message = {
		'extra'   : {'scene': ex},
		'href'    : app.link.share + 'activity=' + view.extras.id,
		'title'   : $('.act-title').text(),
		'content' : $('.act-desc').text(),
	};
	if (~share.id.indexOf('weibo')) {
		message.content += "；体验地址：" + message.href;
	}
	message.thumbs = ["../images/logo.png"];
	share.send(message, function() {
		app.log('分享到"' + share.description + '"成功！ ');
		app.toast('分享成功');
	}, function(e) {
		app.log('分享失败：', e);
		app.toast('分享失败');
	});
}
// 点击分享
$('.btn-share').on('tap', function () {
	var ids = [
		{'id':"weixin", 'ex': "WXSceneSession"},
		{'id':"weixin", 'ex': "WXSceneTimeline"}, 
		{'id':"sinaweibo"}, 
		//{'id':"tencentweibo"}, 
		{'id':"qq"}
	];
	var bts = [
		{title: "发送给微信好友"}, 
		{title: "分享到微信朋友圈"}, 
		{title: "分享到新浪微博"}, 
		//{title: "分享到腾讯微博"}, 
		{title: "分享到QQ"}
	];
	plus.nativeUI.actionSheet({
		cancel: "取消",
		buttons: bts
	}, function(e) {
		if (e.index <= 0) { return; }

		var share = shares[ids[e.index - 1].id];
		if (!share) { app.toast('无法获取分享服务'); }

		if (share.authenticated) {
			_doShareMessage(share, ids[e.index - 1].ex); return;
		}
		share.authorize(function() {
			_doShareMessage(share, ids[e.index - 1].ex);
		}, function(e) {
			app.log('分享认证授权失败：' + e);
			app.toast('分享认证授权失败');
		});
	});
});

// 点赞
$('body').delegate('.btn-awesome', 'tap', function (e) {
	e.stopPropagation();
	var dom = this, view = plus.webview.currentWebview();
	
	plus.nativeUI.showWaiting();
	$(dom).prop('disabled', true);
	$.ajax({
		'dataType' : 'json',
		'type'     : 'post',
		'url'      : app.url('App/Activity/Index/showLike'),
		'data'     : {'token':app.store('token'), 'data':{'activity_show_id':$(dom).closest('.show').attr('data-id')}},
	})
	.fail(function (res) {
		// 失败回调
		plus.nativeUI.closeWaiting();
		app.log('点赞失败', res);
		app.toast('点赞失败');
		$(dom).prop('disabled', false);
	})
	.done(function (res) {
		// 成功回调
		plus.nativeUI.closeWaiting();
		app.log('点赞结果：', res);
		$(dom).prop('disabled', false);
		
		if (500 == res.code) { app.toast('你还未登录'); return; }
		if (300 == res.code) { app.toast(res.msg); return; }
		if (200 != res.code) { app.toast('请求失败'); return; }
		if (res.msg) { app.toast(res.msg); }
		$(dom).prop('disabled', true);
		$(dom).find('i').text(' ' + res.data.likes);
	})
	;
});
// 去秀图详情评论
$('body').delegate('.btn-comment, .show img', 'tap', function (e) {
	e.stopPropagation();
	var dom = this, view = plus.webview.currentWebview();
	var data = {
		'activity' : view.extras.id,
		'show'     : $(dom).closest('.show').attr('data-id'),
	};
	app.open('show.detail.html', data);
});

})(mui, $);
